package fr.mehfile.view;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import fr.mehfile.R;
import fr.mehfile.gallerie.GallerieHandler;

public class GallerieAlbum extends Activity implements View.OnClickListener {
	
	private int indexAlbums = 1;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.galleriealbum);

		final GallerieHandler gallerieHandler = GallerieHandler.getInstance();

		GridView list_album = (GridView) findViewById(R.id.list_album);			
					
		list_album.setAdapter(new BaseAdapter(){
				    	
				    	
						public int getCount() {
							return gallerieHandler.getNumberOfAlbums();
						}

						public Object getItem(int arg0) {
							return null;
						}

						public long getItemId(int arg0) {
							return 0;
						}

						public View getView(final int position, View convertView, ViewGroup parent) {
					        final View viewAlbum;
					        final ImageView imageView;
					        if (convertView == null) {  // if it's not recycled, initialize some attributes
					        	LayoutInflater factory = LayoutInflater.from(GallerieAlbum.this);
					        	viewAlbum = factory.inflate(R.layout.album_item_view, null);
					        	
					        	TextView titreAlbum = (TextView) viewAlbum.findViewById(R.id.titre);
					        	TextView nbrPictures = (TextView) viewAlbum.findViewById(R.id.nbr_picturs);
					        	imageView = (ImageView) viewAlbum.findViewById(R.id.vignette);
					        	
					        	String albumName = gallerieHandler.getAlbumNameByIndex(position);
					        	titreAlbum.setText(albumName);
					        	nbrPictures.setText(gallerieHandler.getAlbumSizeByIndex(position) + " Photo(s)");
					        	
					        	
					        	//viewAlbum.setPadding(2, 2, 2, 10);
					            imageView.setId(GallerieAlbum.this.indexAlbums); 
					            GallerieAlbum.this.indexAlbums++;
					            imageView.setOnClickListener(new OnClickListener() {
									
									public void onClick(View v) {
									
										 Intent intent = new Intent(GallerieAlbum.this,
													GalleriePhotos.class);
											intent.putExtra("albumName", gallerieHandler.getAlbumNameByIndex(position));
											startActivity(intent);
									}
								});
					            imageView.setImageResource(gallerieHandler.getVignette(gallerieHandler.getAlbumNameByIndex(position)));
					        } else {
					        	viewAlbum = (View) convertView;
					        }

					        return viewAlbum;

						}
				    	
				    });
					
	}


	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

}
